//
//  VideoViewController.m
//  UI_Project_A
//
//  Created by GH on 16/3/4.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "VideoViewController.h"
#import "VideoTableViewCell.h"
#import "HandleData.h"
#import "ModelOfVideo.h"
#import "PushVideoController.h"
#import "PushNewVideo.h"
#import <DKNightVersion.h>




@interface VideoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,retain)UITableView *tableview;
@property (nonatomic ,assign)NSInteger number;
@property (nonatomic ,retain)NSMutableArray *arrayData;
@property (nonatomic ,retain)NSMutableArray *arrayVideo;




@end

@implementation VideoViewController


- (void) dealloc {

    [_arrayVideo release];
    [_arrayData release];
    [_tableview release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.number = 0;
    
    [self createView];
    [self data];
    [self createTableView];
    [self createHeadView];

}

- (void) data {
    
   
    
    if (self.arrayVideo == nil) {
        
        self.arrayVideo = [NSMutableArray array];
    }
    
    if (self.arrayData == nil) {
        
        self.arrayData = [NSMutableArray array];
    }
    
    NSString *url = [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/home/%ld-50.html",self.number];
    
    [HandleData handleFloewNewWithString:url Block:^(id result) {
        
        //头部内容
        
      NSArray *arrVideoSidList =  [result objectForKey:@"videoSidList"];
        
        for (NSDictionary *dicVideo in arrVideoSidList) {
            
            
            [self.arrayVideo addObject:[dicVideo objectForKey:@"sid"]];
            
        }
        
        
        
        
     NSArray *array = [result objectForKey:@"videoList"];
        
        for (NSDictionary *dic in array) {
            ModelOfVideo *model = [[ModelOfVideo alloc] init];
            [model setValuesForKeysWithDictionary:dic];
         model.des  =  [dic objectForKey:@"description"];
            
            [self.arrayData addObject:model];
            
        }
        [self.tableview reloadData];
        
    }];
    

    self.number = self.number + 10;




}





- (void)createHeadView {
    //头部view
    
    NSArray *arrimage = @[@"qipa",@"mengchong",@"meinv",@"jingpin"];
    NSArray *arrlabel = @[@"奇葩",@"萌宠",@"美女",@"精品"];
    
    UIView * viewOfhead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 100)];
    
    viewOfhead.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor colorWithWhite:0.247 alpha:1.000]);
    
//    viewOfhead.backgroundColor = [UIColor grayColor];
    self.tableview.tableHeaderView = viewOfhead;
//    [viewOfhead release];
    
    for (int i = 0; i < 4; i++) {
        UIButton *button  = [UIButton buttonWithType:UIButtonTypeCustom];
        [viewOfhead addSubview:button];
        button.frame = CGRectMake(30 +100 *i , 15, 40, 40);
        button.backgroundColor = [UIColor blueColor];
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 20;
        [button setImage:[UIImage imageNamed:arrimage[i]] forState:UIControlStateNormal];
        button.tag = i +1;
        [button addTarget:self action:@selector(pushVC:) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        //label
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30 +100 * i, 60, 40, 30)];
        
        [viewOfhead addSubview:label];
        [label release];
        label.text = arrlabel[i];
        label.textAlignment = 1;
        
        label.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor colorWithWhite:0.772 alpha:1.000]);
        
    }


}

- (void)pushVC: (UIButton *)button {

    if (button.tag == 1) {
        
        PushNewVideo *newVideo = [[PushNewVideo alloc] init];
        newVideo.sid = self.arrayVideo[0];
        newVideo.strTitle = @"奇葩";
        [self presentViewController:newVideo animated:YES completion:nil];
        [newVideo release];
        
    }else if (button.tag == 2) {
    
        PushNewVideo *newVideo = [[PushNewVideo alloc] init];
        newVideo.sid = self.arrayVideo[1];
        newVideo.strTitle = @"萌物";
        [self presentViewController:newVideo animated:YES completion:nil];
        [newVideo release];
    }else if (button.tag == 3) {
    
        PushNewVideo *newVideo = [[PushNewVideo alloc] init];
        newVideo.sid = self.arrayVideo[2];
        newVideo.strTitle = @"美女";
        [self presentViewController:newVideo animated:YES completion:nil];
        [newVideo release];
    }else if (button.tag ==4) {
        PushNewVideo *newVideo = [[PushNewVideo alloc] init];
        newVideo.sid = self.arrayVideo[3];
        newVideo.strTitle = @"精品";
        [self presentViewController:newVideo animated:YES completion:nil];
        [newVideo release];
    }



}



- (void)createTableView {
    
    

    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, WIDTH, HEIGHT - 80) style:UITableViewStylePlain];
    [self.view addSubview:self.tableview];
    [self.tableview release];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.tableview registerClass:[VideoTableViewCell class] forCellReuseIdentifier:@"pool"];



}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arrayData.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    VideoTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"pool"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    
    cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor colorWithWhite:0.247 alpha:1.000]);
    
    ModelOfVideo *model = [self.arrayData objectAtIndex:indexPath.row];
    [cell passModelOfView:model];
    
    return cell;
}


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {


    return  HEIGHT /7 * 4 - 80;
}




- (void)createView {
    
    UIView *viewOfback = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 80)];
    [self.view addSubview:viewOfback];
    viewOfback.backgroundColor = [UIColor redColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH/2 - 50, 20, 100, 50)];
    [viewOfback addSubview:label];
    [label release];
    
    label.text = @"视听";
    label.font = [UIFont systemFontOfSize:25];
    label.textAlignment = 1;
    label.textColor = [UIColor whiteColor];
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [viewOfback addSubview:button];
    [button release];
    button.frame = CGRectMake(20, 20, 50, 50);
    [button setImage:[UIImage imageNamed:@"jiantou3"] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(returnButton:) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)returnButton:(UIButton *)button {


    [self dismissViewControllerAnimated:YES completion:nil];

}





- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    
    
    PushVideoController *push = [[PushVideoController alloc] init];
    push.model =  [self.arrayData objectAtIndex:indexPath.row];
    NSLog(@"--------%@",push.model);
    [self presentViewController:push animated:YES completion:nil];
    [push release];





}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}







/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
